//
//  MainTabBar.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/13.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    //定义一个按钮总数
    private let buttonCount = 5
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        print(__FUNCTION__)
        
        
        let w = bounds.width/CGFloat(buttonCount)
        let rect = CGRectMake(0, 0, w, bounds.height)
        //UITabBarButton 是私有借口  在开发中不要使用
        //遍历所有子视图
        var index = 0
        for v in subviews {
            //判断控件
            if v is UIControl && !(v is UIButton){
                
                v.frame = CGRectOffset(rect, CGFloat(index) * w,
                    0)

                index += index == 1 ? 2 : 1
                
//                print(v.frame)
            }
        }
        //设置加号按钮的位置
      compoesButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    //懒加载 button
    lazy var compoesButton: UIButton = {
        
        let button = UIButton (type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        self.addSubview(button)
        
        return button
        
    }()
    
    

}
